﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataContracts;
using Utility;
using BLL;
using ServiceContracts;
using System.ServiceModel;

namespace ServiceImplementation
{
    /// <summary>
    ///  Implement Service
    /// </summary>
    public class Service : IService
    {
        IRoleRepository roleRepository = RepositoryFactory.SearchTnfo<IRoleRepository>();
        IAuthorityRepository authorityRepository = RepositoryFactory.SearchTnfo<IAuthorityRepository>();
        IUserRepository userRepository = RepositoryFactory.SearchTnfo<IUserRepository>();

        /// <summary>
        /// Get All Role
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<RoleDTO> GetAllRole(string roleName, int status)
        {
            List<BLL.Role> bllrolelist = new List<BLL.Role>();
            List<DataContracts.RoleDTO> rolelistDTO = new List<DataContracts.RoleDTO>();
            bllrolelist = roleRepository.GetAllRole(roleName, status);
            if (bllrolelist != null)
            {
                foreach (BLL.Role allrolelist in bllrolelist)
                {
                    RoleDTO allrolelistDTO = new RoleDTO();
                    allrolelistDTO = ServiceImplementation.Translator.RoleDTOTranslator.TranslateBusinessToDTO(allrolelist);
                    rolelistDTO.Add(allrolelistDTO);
                }
            }
            return rolelistDTO;
        }

        /// <summary>
        /// Get All User Role
        /// </summary>
        /// <returns></returns>
        public List<RoleDTO> GetAllUserRole()
        {
            List<BLL.Role> roleBLL = new List<BLL.Role>();
            List<DataContracts.RoleDTO> roleDTO = new List<DataContracts.RoleDTO>();
            roleBLL = roleRepository.GetAllUserRole();
            if (roleBLL != null)
            {
                foreach (BLL.Role role in roleBLL)
                {
                    RoleDTO dtorole = new RoleDTO();
                    dtorole = ServiceImplementation.Translator.RoleDTOTranslator.TranslateBusinessToDTO(role);
                    roleDTO.Add(dtorole);
                }
            }
            return roleDTO;
        }

        /// <summary>
        /// Delete Role 
        /// </summary>
        /// <param name="roleId"></param>
        public void DeleteRole(int roleId)
        {
            roleRepository.DeleteRole(roleId);
        }

        /// <summary>
        /// Batch Update Role Status
        /// </summary>
        /// <param name="roleList"></param>
        public void BatchUpdateRoleStatus(List<RoleDTO> roleList)
        {
            List<BLL.Role> roleItem = new List<BLL.Role>();
           
            foreach (RoleDTO dto in roleList)
            {

                BLL.Role m = ServiceImplementation.Translator.RoleDTOTranslator.TranslateDTOToBusiness(dto);
                roleItem.Add(m);

            }
            roleRepository.BatchUpdateRoleStatus(roleItem);
        }


        /// <summary>
        /// Get Role by Current RoleId
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public RoleDTO GetRoleById(int roleId)
        {
            BLL.Role myRole = new BLL.Role();
            var bllRole = roleRepository.GetRoleById(roleId);
            DataContracts.RoleDTO roleDTO = ServiceImplementation.Translator.RoleDTOTranslator.TranslateBusinessToDTO(bllRole);
            return roleDTO;
        }

        /// <summary>
        /// Get All Role Status
        /// </summary>
        /// <returns></returns>
        public List<StatusDTO> GetAllRoleStatus()
        {
            List<StatusDTO> statusDTOList = new List<StatusDTO>();
            StatusDTO statusDTO = new StatusDTO();
            List<BLL.Status> statusList = new List<Status>();
            statusList = roleRepository.GetAllRoleStatus();
            foreach (BLL.Status status in statusList)
            {
                statusDTO = ServiceImplementation.Translator.StatusDTOTranslator.TranslateBusinessToDTO(status);
                statusDTOList.Add(statusDTO);
            }
            return statusDTOList;
        }

        /// <summary>
        /// Get Role Status by Current StatusId
        /// </summary>
        /// <param name="statusId"></param>
        /// <returns></returns>
        public StatusDTO GetRoleStatusById(int statusId)
        {
            BLL.Status bllstatus = new BLL.Status();
            bllstatus = roleRepository.GetRoleStatusById(statusId);
            DataContracts.StatusDTO statusDTO = ServiceImplementation.Translator.StatusDTOTranslator.TranslateBusinessToDTO(bllstatus);
            return statusDTO;
        }

        /// <summary>
        /// Update Role
        /// </summary>
        /// <param name="role"></param>
        public void UpdateRole(RoleDTO role)
        {
            BLL.Role bllRole = new BLL.Role();
            bllRole = ServiceImplementation.Translator.RoleDTOTranslator.TranslateDTOToBusiness(role);
            roleRepository.UpdateRole(bllRole);
        }

        /// <summary>
        /// Save a New Role
        /// </summary>
        /// <param name="role"></param>
        public void AddNewRole(RoleDTO role)
        {
            BLL.Role bllRole = new BLL.Role();
            bllRole = ServiceImplementation.Translator.RoleDTOTranslator.TranslateDTOToBusiness(role);
            roleRepository.AddNewRole(bllRole);
        }

        public List<RoleViewDTO> SearchRole(string roleName, string status, string authorities, string sortExpression, string sortDirection, double PageSize, int PageIndex, out int pageCnt)
        {
            List<RoleViewDTO> roleViewDTOList = new List<RoleViewDTO>();
            RoleViewDTO roleViewDTO = new RoleViewDTO();
            List<BLL.Role> bllRoleList = new List<Role>();
            bllRoleList = roleRepository.SearchRole(roleName, status, authorities, sortExpression, sortDirection, PageSize, PageIndex, out pageCnt);
            foreach (BLL.Role role in bllRoleList)
            {
                roleViewDTO = ServiceImplementation.Translator.RoleViewDTOTranslator.TranslateBusinessToDTO(role);
                roleViewDTOList.Add(roleViewDTO);

            }
            return roleViewDTOList;
        }

        /// <summary>
        /// Save a New Authority
        /// </summary>
        /// <param name="authority"></param>
        /// <returns></returns>
        public bool AddNewAuthority(AuthorityDTO authority)
        {
            bool saveResult = false;
            BLL.Authority bllAuthority = new BLL.Authority();
            bllAuthority = ServiceImplementation.Translator.AuthorityDTOTranslator.TranslateDTOToBusiness(authority);
            saveResult = authorityRepository.AddNewAuthority(bllAuthority);
            return saveResult;
        }

        /// <summary>
        /// Delete Authority
        /// </summary>
        /// <param name="authorityId"></param>
        /// <returns></returns>
        public bool DeleteAuthority(int authorityId)
        {
            bool deleteResult = false;
            deleteResult = authorityRepository.DeleteAuthority(authorityId);
            return deleteResult;
        }

        /// <summary>
        /// Update Authority
        /// </summary>
        /// <param name="authority"></param>
        /// <returns></returns>
        public bool UpdateAuthority(AuthorityDTO authority)
        {
            bool updateResult = false;
            BLL.Authority bllAuthority = new BLL.Authority();
            bllAuthority = ServiceImplementation.Translator.AuthorityDTOTranslator.TranslateDTOToBusiness(authority);
            updateResult = authorityRepository.UpdateAuthority(bllAuthority);
            return updateResult;
        }

        /// <summary>
        /// Get Authority by Current AuthorityId
        /// </summary>
        /// <param name="authorityId"></param>
        /// <returns></returns>
        public AuthorityDTO GetAuthorityById(int authorityId)
        {
            BLL.Authority bllAuthority = new BLL.Authority();
            bllAuthority = authorityRepository.GetAuthorityById(authorityId);
            DataContracts.AuthorityDTO authorityDTO = ServiceImplementation.Translator.AuthorityDTOTranslator.TranslateBusinessToDTO(bllAuthority);
            return authorityDTO;
        }

        /// <summary>
        /// Get All Authority Level
        /// </summary>
        /// <returns></returns>
        public List<AuthorityLevelDTO> GetAllAuthorityLevel()
        {
            List<BLL.AuthorityLevel> bllLevelList = new List<BLL.AuthorityLevel>();
            List<DataContracts.AuthorityLevelDTO> dtoLevelList = new List<DataContracts.AuthorityLevelDTO>();
            bllLevelList = authorityRepository.GetAllAuthorityLevel();
            if (bllLevelList != null)
            {
                foreach (BLL.AuthorityLevel level in bllLevelList)
                {
                    AuthorityLevelDTO dtoLevel = new AuthorityLevelDTO();
                    dtoLevel = ServiceImplementation.Translator.AuthorityLevelDTOTranslator.TranslateBusinessToDTO(level);
                    dtoLevelList.Add(dtoLevel);
                }
            }
            return dtoLevelList;
        }

        /// <summary>
        /// Get All Authority
        /// </summary>
        /// <returns></returns>
        public List<AuthorityDTO> GetAllAuthority()
        {
            List<BLL.Authority> bllAuthorityList = new List<BLL.Authority>();
            List<DataContracts.AuthorityDTO> dtoAuthorityList = new List<DataContracts.AuthorityDTO>();
            bllAuthorityList = authorityRepository.GetAllAuthority();
            if (bllAuthorityList != null)
            {
                foreach (BLL.Authority authority in bllAuthorityList)
                {
                    AuthorityDTO dtoAuthority = new AuthorityDTO();
                    dtoAuthority = ServiceImplementation.Translator.AuthorityDTOTranslator.TranslateBusinessToDTO(authority);
                    dtoAuthorityList.Add(dtoAuthority);
                }
            }
            return dtoAuthorityList;
        }

        /// <summary>
        /// Get Search Authority
        /// </summary>
        /// <returns></returns>
        public List<AuthorityDTO> SearchAuthority(string authorityName , int levelId)
        {
            List<BLL.Authority> bllAuthorityList = new List<BLL.Authority>();
            List<DataContracts.AuthorityDTO> dtoAuthorityList = new List<DataContracts.AuthorityDTO>();
            bllAuthorityList = authorityRepository.SearchAuthority(authorityName, levelId);
            if (bllAuthorityList != null)
            {
                foreach (BLL.Authority authority in bllAuthorityList)
                {
                    AuthorityDTO dtoAuthority = new AuthorityDTO();
                    dtoAuthority = ServiceImplementation.Translator.AuthorityDTOTranslator.TranslateBusinessToDTO(authority);
                    dtoAuthorityList.Add(dtoAuthority);
                }
            }
            return dtoAuthorityList;
        }

        /// <summary>
        /// Save a New User
        /// </summary>
        /// <param name="userDTO"></param>
        /// <returns></returns>
        public bool AddNewUser(UserDTO userDTO)
        {
            BLL.User userBll = new BLL.User();
            userBll = ServiceImplementation.Translator.UserDTOTranslator.TranslateDTOToBusiness(userDTO);
            return userRepository.AddNewUser(userBll);
        }

        /// <summary>
        /// Delete User
        /// </summary>
        /// <param name="userId"></param>
        public void DeleteUser(int userId)
        {
            userRepository.DeleteUser(userId);
        }

        /// <summary>
        /// Update User
        /// </summary>
        /// <param name="userDTO"></param>
        public void UpdateUser(UserDTO userDTO)
        {
            BLL.User userBll = new BLL.User();
            userBll = ServiceImplementation.Translator.UserDTOTranslator.TranslateDTOToBusiness(userDTO);
            userRepository.UpdateUser(userBll);
        }

        /// <summary>
        /// Get All User
        /// </summary>
        /// <returns></returns>
        public List<UserDTO> GetAllUser()
        {
            List<DataContracts.UserDTO> listDTO = new List<DataContracts.UserDTO>();
            List<BLL.User> listBLL = new List<BLL.User>();
            listBLL = userRepository.GetAllUser().ToList();
            if (listBLL != null)
            {
                foreach (BLL.User userBll in listBLL)
                {
                    DataContracts.UserDTO userDTO = ServiceImplementation.Translator.UserDTOTranslator.TranslateBusinessToDTO(userBll);
                    listDTO.Add(userDTO);
                }
            }
            return listDTO;
        }

        /// <summary>
        /// Get All Department
        /// </summary>
        /// <returns></returns>
        public List<DepartmentDTO> GetAllDepartment()
        {
            List<DataContracts.DepartmentDTO> listDTO = new List<DataContracts.DepartmentDTO>();
            List<BLL.Department> listBLL = userRepository.GetAllDepartment().ToList();
            if (listBLL != null)
            {
                foreach (BLL.Department departmentBll in listBLL)
                {
                    DataContracts.DepartmentDTO departmentDTO = ServiceImplementation.Translator.DepartmentDTOTranslator.TranslateBusinessToDTO(departmentBll);
                    listDTO.Add(departmentDTO);
                }
            }
            return listDTO;
        }

        /// <summary>
        /// Get User by Current UserId
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserDTO GetUserById(int userId)
        {
            BLL.User userBll = new BLL.User();
            userBll = userRepository.GetUserById(userId);
            DataContracts.UserDTO userDTO = ServiceImplementation.Translator.UserDTOTranslator.TranslateBusinessToDTO(userBll);
            return userDTO;
        }

        /// <summary>
        /// Search User by Complex Conditions
        /// </summary>
        /// <param name="departmentId"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<UserDTO> SearchUser(int departmentId, string firstName, string lastName)
        {
            List<DataContracts.UserDTO> listDTO = new List<DataContracts.UserDTO>();
            List<BLL.User> listBLL = userRepository.SearchUser(departmentId, firstName , lastName ).ToList();
            if (listBLL != null)
            {
                foreach (BLL.User userBll in listBLL)
                {
                    DataContracts.UserDTO userDTO = ServiceImplementation.Translator.UserDTOTranslator.TranslateBusinessToDTOView(userBll);
                    listDTO.Add(userDTO);
                }
            }
            return listDTO;
        }
    }
}
